package cc.lj.internal.coupons.domain.martcap.service.impl;

import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.coupons.domain.martcap.model.vo.MartCapDO;
import cc.lj.internal.coupons.domain.martcap.model.vo.MultipleModelDO;
import cc.lj.internal.coupons.domain.martcap.model.vo.convert.MultipleModelDOConvertPO;
import cc.lj.internal.coupons.domain.martcap.repository.IMultipleModelRepository;
import cc.lj.internal.coupons.domain.martcap.service.ICapMerchantDomainService;
import cc.lj.internal.coupons.domain.martcap.service.IMartCapDomainService;
import cc.lj.internal.coupons.domain.martcap.service.IMultipleModelDomainService;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.infrastructure.po.MultipleModelPO;
import cc.lj.internal.coupons.interfaces.dto.MultipleModelDTO;
import cc.lj.internal.coupons.interfaces.dto.command.CapMerchantClearValueCommand;
import cc.lj.internal.coupons.interfaces.dto.query.MultipleModelListQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class IMultipleModelDomainServiceImpl implements IMultipleModelDomainService {

    @Resource(name = "multipleModelRepository")
    private IMultipleModelRepository multipleModelRepository;
    @Autowired
    private IMartCapDomainService martCapDomainService;
    @Autowired
    private ICapMerchantDomainService capMerchantDomainService;

    @Override
    public Integer insert(MultipleModelDO multipleModelDO) {
        Integer id = multipleModelRepository.insert(MultipleModelDOConvertPO.INSTANCE.convert(multipleModelDO));
        return id;
    }

    @Override
    public MultipleModelDO getById(Integer id) {
        MultipleModelPO multipleModelPO = multipleModelRepository.getById(id);
        return MultipleModelDOConvertPO.INSTANCE.convert(multipleModelPO);
    }

    /**
     * 修改倍数模型，同时需要将cap_merchant表中对应商户封顶额清零
     * @param multipleModelDO
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public Integer updateById(MultipleModelDO multipleModelDO) {
        //查询封顶额活动表信息
        MartCapDO martCapDO_mysql = martCapDomainService.getById(multipleModelDO.getCapId());
        //活动已经开始，不能修改倍数模型
        if(martCapDO_mysql.getIsStart()==1)
        {
            throw new ServiceException(CoupousExceptionEnum.MULITPLE_UPDATE_MARTCAP_ERROR);
        }
        //更新倍数表
        Integer row = multipleModelRepository.updateById(MultipleModelDOConvertPO.INSTANCE.convert(multipleModelDO));
        //更新封顶额商户表
        CapMerchantClearValueCommand capMerchantClearValueCommand = new CapMerchantClearValueCommand();
        capMerchantClearValueCommand.setModelId(multipleModelDO.getId());
        Integer merchant_row = capMerchantDomainService.clearMerchantValue(capMerchantClearValueCommand);
        return row;
    }

    /**
     * 获取列表（无分页）
     * @param
     * @return
     */
    @Override
    public List<MultipleModelDO> getList(MultipleModelDO multipleModelDO) {
        List<MultipleModelPO> list = multipleModelRepository.getList(MultipleModelDOConvertPO.INSTANCE.convert(multipleModelDO));
        return list.stream().map(t -> MultipleModelDOConvertPO.INSTANCE.convert(t)).collect(Collectors.toList());
    }

}
